<?php
namespace Youni\Controller;
use Think\Controller;
use Think\Upload;

class LinkController extends YnController {
    public function index(){
		$mod = M('link')->order("id ASC") -> select();
        $mod = list_qiniu_cdn_url($mod);
		$this -> assign("mod",$mod);
		$config =M("config")->where("id=1")->find()['webname'];
		$this->assign("config",$config);
        $this->display("index");
    }

	public function insert() {
		if(!empty($_FILES)) {
			//如果有文件上传 上传附件
			 $this->_upload();
			//$this->forward();
		}

		parent::insert();
	}

	public function edit(){
		$id=$_GET['id'];
		$mod = M('link')->where("id={$id}")->find();
        $mod['picname'] = get_qiniu_cdn_url($mod['picname']);
		$this->assign("mod",$mod);
		$config =M("config")->where("id=1")->find()['webname'];
		$this->assign("config",$config);
	   $this->display("edit");
	}
	 public function add() {
		$config =M("config")->where("id=1")->find()['webname'];
		$this->assign("config",$config);
		$this->display("add");
	}


		//执行修改
    public function update() {
		if(!empty($_FILES) && $_FILES["uploadpic"]["error"]==0){
			//如果有文件上传 上传附件
			 $this->_upload();
			//$this->forward();
			//删除原图
			$model = D("Adver");
		    $id = $_REQUEST ["id"];
			$ob = $model->find($id);
			if(!empty($ob)){
				@unlink('./Public/uploads/'.$ob['picname']);
				//@unlink('./Public/uploads/links/s_'.$ob['logo']);
			}
		}
		parent::update();
	}


	public function tans(){
		$id = $_GET['id'];
		M('link')->where("id='{$id}'")->save(['status'=>1]);
		$mod = M('link')->order("id ASC")-> select();
		$this -> assign("mod",$mod);
		$this->display("index");
	}
	 public function guan() {
		$id = $_GET['id'];
		M('link')->where("id='{$id}'")->save(['status'=>2]);
		$mod = M('link')->order("id ASC")-> select();
		$this -> assign("mod",$mod);
		$this->display("index");
	}


	// 文件上传
    protected function _upload()
    {
        $setting = C('UPLOAD_SITEIMG_QINIU');
        $upload = new \Think\Upload($setting);// 实例化上传类
        $info = $upload->upload();
        if(!$info) {
            $this->error($upload->getError());
        }else {
            //取得成功上传的文件信息
            $uploadList = array_values($info);
            $picname = $uploadList[0]['name'];
            $picname=str_replace('/','_',$picname);
			$_POST['picname']  = $picname;
		}
	}
}
